package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.template.biz.service.sys.SysDeptService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.convert.sys.SysDeptConvert;
import com.liliudong.template.common.dto.sys.SysDeptAddDTO;
import com.liliudong.template.common.dto.sys.SysDeptUpdateDTO;
import com.liliudong.template.common.entity.sys.SysDept;
import com.liliudong.template.common.vo.sys.SysDeptBaseVO;
import com.liliudong.template.common.vo.sys.SysDeptDetailVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统部门
 *
 * @author ldli5
 * @classname SysDeptController
 * @date 2024/6/21-8:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/dept")
public class SysDeptController {

	private final SysDeptService sysDeptService;

	/**
	 * 添加一个
	 *
	 * @param sysDeptAddDTO 系统部门添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysDeptAddDTO sysDeptAddDTO) {
		sysDeptService.addOne(sysDeptAddDTO);
		return R.ok();
	}

	/**
	 * 按id删除
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:delete")
	@DeleteMapping("/{id}")
	public R<?> deleteById(@PathVariable Long id) {
		sysDeptService.deleteById(id);
		return R.ok();
	}

	/**
	 * 按id修改
	 *
	 * @param id 身份证件
	 * @param sysDeptUpdateDTO 系统部门更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:update")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id,
	                       @RequestBody @Validated SysDeptUpdateDTO sysDeptUpdateDTO) {
		sysDeptService.modifyById(id, sysDeptUpdateDTO);
		return R.ok();
	}

	/**
	 * 树
	 *
	 * @return {@link R }<{@link List }<{@link Tree }<{@link Long }>>>
	 */
	@SaCheckPermission("sys:dept:list")
	@GetMapping("/tree")
	public R<List<Tree<Long>>> tree() {
		List<Tree<Long>> tree =  sysDeptService.tree();
		return R.ok(tree);
	}

	/**
	 * 列出所有
	 *
	 * @return {@link R }<{@link List }<{@link SysDept }>>
	 */
	@SaCheckPermission("sys:dept:list")
	@GetMapping("/all")
	public R<List<SysDeptBaseVO>> listAll() {
		List<SysDept> sysDeptList = sysDeptService.list();
		return R.ok(SysDeptConvert.INSTANCE.entity2BaseVO(sysDeptList));
	}

	/**
	 * id明细
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:detail")
	@GetMapping("/detail/{id}")
	public R<?> detailById(@PathVariable Long id) {
		SysDeptDetailVO sysDeptDetailVO = sysDeptService.detailById(id);
		return R.ok(sysDeptDetailVO);
	}
}
